﻿#include <QTimer>
#include <QThread>
#include <QDebug>

#include "common/LoopBuffer.h"

#include "AudioDataDispatcher.h"

#ifdef WIN32
#include "render/WinAudioRender.h"
#include "capture/win/WinMediaDevice.h"
#include "capture/win/WinSpeakerRecord.h"
#include "capture/win/WinMicRecord.h"
#else
#include "capture/mac/MacMicRecord.h"
#include "capture/mac/MacMediaDevice.h"
#include "capture/mac/MacMicRecord.h"
#endif

#include "RecordManager.h"

RecordManager::RecordManager(QObject *parent) : QObject(parent)
{
    qDebug() << QThread::currentThread();

#ifdef WIN32
    /* 此deviceid要求和spk一致 */
    QThread *renderThread = new QThread;
    WinAudioRender *render = new WinAudioRender(0);
    render->moveToThread(renderThread);
    renderThread->start();
    QTimer *renderTimer = new QTimer;
    renderTimer->setSingleShot(true);
    connect(renderTimer,&QTimer::timeout,render,&WinAudioRender::run);
    renderTimer->start();


    QThread * speakerThread = new QThread;
    WinSpeakerRecord *speakerThreadObj = new WinSpeakerRecord;
    speakerThreadObj->moveToThread(speakerThread);
    speakerThread->start();

    QTimer *speakerTimer = new QTimer;
    speakerTimer->setSingleShot(true);
    QObject::connect(speakerTimer,&QTimer::timeout,speakerThreadObj,&WinSpeakerRecord::run);

    QThread * micThread = new QThread;
    WinMicRecord *micThreadObj = new WinMicRecord;
    micThreadObj->moveToThread(micThread);
    micThread->start();

    QTimer *micTimer = new QTimer;
    micTimer->setSingleShot(true);
    QObject::connect(micTimer,&QTimer::timeout,micThreadObj,&WinMicRecord::run);

    QThread *audioThread = new QThread;
    AudioDataDispatcher *audioThreadObj = new AudioDataDispatcher;

    audioThreadObj->moveToThread(audioThread);
    audioThread->start();

    QTimer *mp3Timer = new QTimer;
    mp3Timer->setSingleShot(true);
    QObject::connect(mp3Timer,&QTimer::timeout,audioThreadObj,&AudioDataDispatcher::run);


    QObject::connect(micThreadObj,&WinMicRecord::audioData,audioThreadObj,&AudioDataDispatcher::acceptMicData);

    QObject::connect(speakerThreadObj,&WinSpeakerRecord::audioData,audioThreadObj,&AudioDataDispatcher::acceptSpeakerData);

    qDebug() << "main thread" << QThread::currentThreadId();
    speakerTimer->start(0);
    mp3Timer->start(0);
    micTimer->start(0);

    qDebug() << WinMediaDevice::getDeviceList(WinMediaDevice::MediaDeviceType_Input);
    qDebug() << WinMediaDevice::getDeviceList(WinMediaDevice::MediaDeviceType_Output);
#else
    qDebug() << MacMediaDevice::getDeviceList(MacMediaDevice::MediaDeviceType_Input);
    qDebug() << MacMediaDevice::getDeviceList(MacMediaDevice::MediaDeviceType_Output);

    QThread *micThread = new QThread;
    MacAudio *record = new MacAudio;
    record->setDeviceId("51");
    record->moveToThread(micThread);
    micThread->start();

    QThread *audioThread = new QThread;
    AudioDataDispatcher *audioThreadObj = new AudioDataDispatcher;

    audioThreadObj->moveToThread(audioThread);
    audioThread->start();

    QTimer *micTimer = new QTimer;
    micTimer->setSingleShot(true);
    connect(micTimer,&QTimer::timeout,record,&MacAudio::run);
    micTimer->start(0);

    QTimer *mp3Timer = new QTimer;
    mp3Timer->setSingleShot(true);

    QObject::connect(mp3Timer,&QTimer::timeout,audioThreadObj,&AudioDataDispatcher::run);
    connect(record,&MacAudio::GotAVFrame,audioThreadObj,&AudioDataDispatcher::acceptMacMicData);

    mp3Timer->start();

#endif
}
